package com.itheima.dbservice;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pojo.User;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getByPhone(String phone) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(phone), User::getMobile, phone);
        return userMapper.selectOne(lqw);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public void updateInfo(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User getUser(String uid) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getId, uid);
        return userMapper.selectOne(lqw);
    }

    @Override
    public Long getCount() {
        return userMapper.getCount();
    }

    @Override
    public User getByHuanxinId(String huanxinId) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getHxUser, huanxinId);
        return userMapper.selectOne(lqw);
    }
}
